package cn.com.mqtt.consumer;
 

import javax.annotation.PostConstruct;
 
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import lombok.extern.slf4j.Slf4j;
 
@Configuration
@Slf4j
public class MqttConsumerConfig {
    @Value("${spring.mqtt.username}")
    private String username;
 
    @Value("${spring.mqtt.password}")
    private String password;
 
    @Value("${spring.mqtt.url}")
    private String hostUrl;
 
    @Value("${spring.mqtt.client.id}")
    private String clientId;
 
    @Value("${spring.mqtt.default.topic}")
    private String defaultTopic;
    
    @Value("${spring.mqtt.default.qos}")
    private String defaultQos;
    
    /*private static GasHardwareService gasHardwareService;
	
	@Autowired
	public void setGasHardwareService(GasHardwareService gasHardwareService){
		MqttConsumerConfig.gasHardwareService = gasHardwareService;
	}*/
    
    /**
     * 客户端对象
     */
    private MqttClient client;
 
    /**
     * 在bean初始化后连接到服务器
     */
    @PostConstruct
    public void init(){
        if(connect()){
        	log.info("项目启动连接MQTT成功！");
        }else{
        	log.info("项目启动连接MQTT失败！");
        };
    }
    
    
    /**
     * 客户端连接服务端
     */
    public Boolean connect(){
        try {
            //创建MQTT客户端对象
            client = new MqttClient(hostUrl,clientId,new MemoryPersistence());
        	//连接设置
            MqttConnectOptions options = new MqttConnectOptions();
            //是否清空session，设置为false表示服务器会保留客户端的连接记录，客户端重连之后能获取到服务器在客户端断开连接期间推送的消息
            //设置为true表示每次连接到服务端都是以新的身份
            options.setCleanSession(true);
            
            //设置连接用户名
            options.setUserName(username);
            //设置连接密码
            options.setPassword(password.toCharArray());
            
            // 重连设置
            options.setAutomaticReconnect(false);
            
            //设置超时时间，单位为秒
            options.setConnectionTimeout(100);
            //设置心跳时间 单位为秒，表示服务器每隔1.5*20秒的时间向客户端发送心跳判断客户端是否在线
            options.setKeepAliveInterval(20);
            //设置遗嘱消息的话题，若客户端和服务器之间的连接意外断开，服务器将发布客户端的遗嘱信息
            options.setWill("willTopic",(clientId + "与服务器断开连接").getBytes(),0,false);
            //设置回调
            client.setCallback(new MqttConsumerCallBack());
            client.connect(options);
            //订阅主题
            //消息等级，和主题数组一一对应，服务端将按照指定等级给订阅了主题的客户端推送消息
            
            //主题
            int[] qos = {};
            String[] topics = {};

            /*List<GasHardwareEntity> list = gasHardwareService.list();
            
            if(TsBqUtils.isNotNull(list)){
            	int _i = 0;
            	for (GasHardwareEntity gasHardwareEntity : list) {
            		String _topic = gasHardwareEntity.getTopic();
            		String _qos = gasHardwareEntity.getQos();
            		if(TsBqUtils.isNotNull(_topic)&&TsBqUtils.isNotNull(_qos)){
            			qos[_i] = Integer.parseInt(_qos);
            			topics[_i] = _topic;
            			_i ++;
            		}
				}
            	
            }else{
            	 topics = defaultTopic.split(",");
            	 String[] qosStr = defaultQos.split(",");
            	 qos = new int[qosStr.length];
                 for (int i = 0; i < qosStr.length; i++) {
                 	qos[i] = Integer.parseInt(qosStr[i]);
                 }
            }*/
            
            topics = defaultTopic.split(",");
       	 	String[] qosStr = defaultQos.split(",");
       	 	qos = new int[qosStr.length];
            for (int i = 0; i < qosStr.length; i++) {
            	qos[i] = Integer.parseInt(qosStr[i]);
            }
            
            //订阅主题
            client.subscribe(topics,qos);
            return true;
        } catch (MqttException e) {
            e.printStackTrace();
            log.info(e.getMessage());
            return false;
        }
		
    }
 
    /**
     * 断开连接
     */
    public void disConnect(){
        try {
            client.disconnect();
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }
 
 
    /**
     * 订阅主题
     */
    public void subscribe(String topic,int qos){
        try {
            client.subscribe(topic,qos);
        } catch (MqttException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * 判断是否断开连接
     * @return
     */
    public Boolean isConnected(){
		return client.isConnected();
    }
   
}